import { defineConfig } from 'vite' // 动态配置函数
import vue from '@vitejs/plugin-vue2'
import { resolve } from 'path'
// import requireTransform from 'vite-plugin-require-transform' // 1. 引入插件

export default ({ mode }) => {
  // let base = "/manage/";
  // let outDir = "dist";
  // let port = 1588;
  // console.log(mode,'mode')
  // if (mode === "dev") {
  //   base = "/manage/v2/dev/";
  //   outDir = "distDev";
  //   port = 1567;
  // }
  let base = ''
  let outDir = 'dist'
  let port = 1588
  if (mode === 'dev') {
    base = ''
    outDir = 'distDev'
    port = 1570
  }

  return defineConfig({
    plugins: [vue()],
    server: {
      open: true, //自动打开浏览器
      port, //端口号
      host: '0.0.0.0',
      proxy: {
        '/apidev': {
          target: 'https://shelvesserver.jiachuangnet.com/apidev',
          // target:"https://selectionserver.jiachuangnet.com/apidev",//零食优选
          // target: 'http://192.168.2.144:8088/', //biao
          // target: "http://192.168.2.152:8088/",//jie
          // target: 'http://192.168.2.157:8088/', //hao
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/apidev/, ''),
        },
        '/api': {
          target: 'http://jiachuangnet.com/api',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    base,
    optimizeDeps: {
      // 开发时 解决这些commonjs包转成esm包
      include: [
        '@jiaminghi/c-render',
        '@jiaminghi/c-render/lib/plugin/util',
        '@jiaminghi/charts/lib/util/index',
        '@jiaminghi/charts/lib/util',
        '@jiaminghi/charts/lib/extend/index',

        '@jiaminghi/charts',
        '@jiaminghi/color',
      ],
    },
    build: {
      outDir,
      commonjsOptions: {
        include: [
          /node_modules/, // 必须包含
        ],
      },
      rollupOptions: {
        input: {
          main: resolve(__dirname, 'index.html'),
          nested: resolve(__dirname, 'nested/index.html'),
        },
      },
    },
    resolve: {
      // 别名
      alias: [
        {
          find: '@',
          replacement: '/src',
        },
        {
          find: '@js',
          replacement: '/src/assets/js',
        },
      ],
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import "./src/assets/css/_var.less";',
        },
      },
    },
  })
}
